package com.jason.quectel.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.jason.quectel.R;
import com.jason.quectel.utils.OkHttpUtils;
import com.jason.quectel.utils.SPUtils;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private String imageUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.3dmgame.com%2Fuploads%2Fimages%2Fnews%2F20210519%2F1621415456_116908" +
            ".jpg&refer=http%3A%2F%2Fimg.3dmgame.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649473170&t=6ca717a24d98e8da71f8969f53501e6e";
    private ImageView imageView;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Log.d(TAG, "handleMessage: " + msg.obj);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);

        SPUtils.init(this);
        SPUtils.put("name", "jason");

        Log.d(TAG, "onCreate: " + SPUtils.get("name", "nothing"));
    }

    private void getNetData() {
        ExecutorService fixedThreadPool = new ThreadPoolExecutor(
                5,
                5,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue());
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                String address = "https://www.baidu.com/";
                try {
                    Response response = OkHttpUtils.get(address);
                    String result = response.body().string();
                    //String result = HttpUtils.get(address);
                    Log.d(TAG, "请求成功\r\n" + result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void getImageUrl(String url) {
        ExecutorService fixedThreadPool = new ThreadPoolExecutor(
                5,
                5,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue());
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Response response = OkHttpUtils.get(url);
                    byte[] bytes = response.body().bytes();
                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageBitmap(bitmap);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void goSecond(View view) {
        //getNetData();

        //Message message = Message.obtain();
        //message.what = 0;
        //message.setTarget(handler);
        //message.obj = "quectel";
        //handler.sendMessage(message);

        //Intent intent = new Intent();
        //intent.setAction(Intent.ACTION_VIEW);
        //intent.setData(Uri.parse("http://www.quectel.com"));
        //intent.setAction(Intent.ACTION_DIAL);
        //intent.setData(Uri.parse("tel://18272164317"));
        //intent.setType("text/html");
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //intent.putExtra("key", "value");
        //startActivity(intent);

        startActivity(new Intent(this, AnimationTestActivity.class));

        //getImageUrl(imageUrl);
        //Glide.with(this).load(imageUrl).into(imageView);
    }
}